﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class ManagerToRoleController : UI.BaseController
    {

        public const string ProgrameCode = "ManagerToRole";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public ManagerToRoleController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index(string UserName,string RoleCode)
        {
            ViewBag.UserName = UserName;
            ViewBag.RoleCode=RoleCode;
            if (!string.IsNullOrEmpty(UserName))
            {
                ViewBag.ListParams += "?UserName=" + UserName;
                RoleCode = null;
            }
            if (!string.IsNullOrEmpty(RoleCode))
            {
                ViewBag.ListParams += "?RoleCode=" + RoleCode;
            }
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(int page, int rows, string sort, string order)
        {
            var bll = new BLL.ManagerToRole();
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.ManagerToRole), "UserName,Manager.RealName,RoleCode,ManagerRole.RoleName");
            string orderby = null;
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    ID = i.ID,
                    UserName = i.Manager.UserName,
                    RealName = i.Manager.RealName,
                    RoleCode = i.ManagerRole.RoleCode,
                    RoleName = i.ManagerRole.RoleName
                });
            }
            var ret = new
            {
                total = bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.ManagerToRole().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode, "add")]
        public ViewResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string Add(string UserName, string RoleCode)
        {
            var ret = new BLL.ManagerToRole().Add(UserName, RoleCode);
            return UI.Utils.ToJsonString(ret);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string AddUser(string UserName, string RoleCode)
        {
            var ret = new BLL.ManagerToRole().AddUser(UserName,RoleCode);
            return UI.Utils.ToJsonString(ret);

        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string AddRole(string UserName, string RoleCode)
        {
            var ret = new BLL.ManagerToRole().AddRole(UserName, RoleCode);
            return UI.Utils.ToJsonString(ret);

        }
    }
}
